from pydantic import BaseModel, Field
from datetime import datetime
from typing import Optional, List, Dict, Any, Generic, TypeVar
from pydantic import validator

# 用于泛型参数的类型变量
T = TypeVar('T')

class PaginatedResponse(BaseModel, Generic[T]):
    """通用分页响应模型"""
    total: int = Field(..., description="总记录数")
    page: int = Field(..., description="当前页码")
    page_size: int = Field(..., description="每页大小")
    items: List[T] = Field(..., description="分页数据列表")

class FileResponse(BaseModel):
    """文件响应模型"""
    id: int = Field(..., description="文件ID")
    user_id: Optional[int] = Field(None, description="上传用户的ID")
    file_name: Optional[str] = Field(None, description="文件原始名称")
    file_path: Optional[str] = Field(None, description="MinIO存储路径")
    file_type: Optional[str] = Field(None, description="文件类型（photo、video、audio、document）")
    file_size: Optional[int] = Field(None, description="文件大小（字节）")
    hash: Optional[str] = Field(None, description="文件哈希值（SHA256）")
    upload_time: Optional[datetime] = Field(None, description="文件上传时间")
    create_time: Optional[datetime] = Field(None, description="文件创建时间")
    extension: Optional[str] = Field(None, description="文件扩展名")
    mime_type: Optional[str] = Field(None, description="MIME类型")
    remark: Optional[str] = Field(None, description="额外备注信息")
    is_deleted: bool = Field(False, description="是否已删除")
    directory_id: Optional[int] = Field(None, description="所属目录ID")

    class Config:
        """Pydantic配置"""
        from_attributes = True  # 允许从ORM模型创建

class FileVerifyRequest(BaseModel):
    """文件哈希验证请求"""
    file_hash: Optional[str] = Field(None, description="单个文件哈希值（SHA256）")
    file_hashes: Optional[List[str]] = Field(None, description="批量文件哈希值列表（SHA256）")
    
    class Config:
        schema_extra = {
            "example": {
                "file_hashes": [
                    "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
                    "a1b2c3d4e5f6g7h8i9j0k1l2m3n4o5p6q7r8s9t0u1v2w3x4y5z6a7b8c9d0e1f2"
                ]
            }
        }
        
    @validator('file_hashes', 'file_hash')
    def validate_hash_params(cls, v, values):
        # 确保至少提供了一个哈希参数
        if 'file_hash' in values and values['file_hash'] is None and (v is None or len(v) == 0):
            raise ValueError("至少需要提供file_hash或file_hashes中的一个")
        return v

class FileVerifyResponse(BaseModel):
    """文件哈希验证响应"""
    exists: bool = Field(..., description="文件是否已存在")
    file_id: Optional[int] = Field(None, description="如果存在，文件的ID")
    upload_permission: bool = Field(..., description="是否允许上传")
    message: str = Field(..., description="提示信息")

class BatchFileVerifyResult(BaseModel):
    """批量文件哈希验证结果项"""
    file_hash: str = Field(..., description="文件哈希值")
    exists: bool = Field(..., description="文件是否已存在")
    file_id: Optional[int] = Field(None, description="如果存在，文件的ID")
    upload_permission: bool = Field(..., description="是否允许上传")

class BatchFileVerifyResponse(BaseModel):
    """批量文件哈希验证响应"""
    results: List[BatchFileVerifyResult] = Field(..., description="验证结果列表")
    total: int = Field(..., description="总验证数量")
    exists_count: int = Field(..., description="已存在文件数量") 